layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //评论列表
    var tableIns = table.render({
        elem: '#newsList',
        url : '/personal_blog/bbses/listByPage/notReview',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        limits : [10,15,20,25],
        id : "newsListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', title: 'ID', width:60, align:"center"},
            {field: 'bbsUserIp', title: '用户ip', width:150, align:'center'},
            {field: 'bbsContent', title: '评论内容', width:600},
            {field: 'bbsDate', title: '评论日期', width:150, align:'center', templet:function(d){
                return moment(d.commentDate).format('YYYY-MM-DD HH:mm:ss').substring(0,10);
            }}

        ]]
    });

    //审核不通过
    $(".commentIsNotPass").click(function(){
        var checkStatus = table.checkStatus('newsListTable'),
            data = checkStatus.data,
            newsId = [];
        if(data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].id);
            }
            layer.confirm('选中的评论审核不给通过？', {icon: 3, title: '提示信息'}, function (index) {
                var url ="/personal_blog/bbses/bbsIsNotPass";
                $.ajax({
                    type:"put",
                    data:{
                        ids:newsId
                    },
                    traditional:true,
                    url:url,
                    success:function () {
                        tableIns.reload();
                        layer.close(index);
                        setTimeout(function(){
                            layer.closeAll("iframe");
                            window.parent.location.reload();//刷新父页面
                            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.layer.close(index);
                        },0.0001);
                    }
                })
            })
        }else{
            layer.msg("请选择审核不通过的评论");
        }
    })

    //审核通过
    $(".commentIsPass").click(function(){
        var checkStatus = table.checkStatus('newsListTable'),
            data = checkStatus.data,
            newsId = [];
        if(data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].id);
            }

            layer.confirm('选中的评论审核通过？', {icon: 3, title: '提示信息'}, function (index) {
                var url ="/personal_blog/bbses/bbsIsPass";
                $.ajax({
                    type:"put",
                    data:{
                        ids:newsId
                    },
                    traditional:true,
                    url:url,
                    success:function () {
                        tableIns.reload();
                        layer.close(index);

                        setTimeout(function(){
                            layer.closeAll("iframe");
                            window.parent.location.reload();//刷新父页面
                            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.layer.close(index);
                        },0.0001);
                    }
                })
            })
        }else{
            layer.msg("请选择审核通过的评论");
        }
    })





    //列表操作
    table.on('tool(newsList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addNews(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此文章？',{icon:3, title:'提示信息'},function(index){
                var url = "/personal_blog/blogs/"+data.id;
                axios.put(url+"/delete").then(function (response) {
                    console.log(response.data)
                    tableIns.reload();
                    layer.close(index);
                })

                // $.get("删除文章接口",{
                //     newsId : data.newsId  //将需要删除的newsId作为参数传入
                // },function(data){
                //     tableIns.reload();
                //     layer.close(index);
                // })
            });
        } else if(layEvent === 'look'){ //预览
            layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问")
        }
    });



    function getUrlParms(para){
        var search=location.search; //页面URL的查询部分字符串
        var arrPara=new Array(); //参数数组。数组单项为包含参数名和参数值的字符串，如“para=value”
        var arrVal=new Array(); //参数值数组。用于存储查找到的参数值

        if(search!=""){
            var index=0;
            search=search.substr(1); //去除开头的“?”
            arrPara=search.split("&");

            for(i in arrPara){
                var paraPre=para+"="; //参数前缀。即参数名+“=”，如“para=”
                if(arrPara[i].indexOf(paraPre)==0&& paraPre.length<arrPara[i].length){
                    arrVal[index]=decodeURI(arrPara[i].substr(paraPre.length)); //顺带URI解码避免出现乱码
                    index++;
                }
            }
        }

        if(arrVal.length==1){
            return arrVal[0];
        }else if(arrVal.length==0){
            return null;
        }else{
            return arrVal;
        }
    }







})